import { Time } from "../../Time/Time";
import { AICom } from "../Entity/AICom";
import { AIDispatcherCom } from "../Entity/AIDipatcherCom";
@et.objectSystem(AICom)
class AIComUpdateSystem extends et.UpdateSystem<AICom>{
    update(self: AICom): void {
        self.CheckIntervalTemp -= Time.deltaTime;
        if (self.CheckIntervalTemp <= 0) {
            self.CheckIntervalTemp = self.CheckInterval;
            AIComHelper.Check(self);
        }
    }
}
@et.objectSystem(AICom)
class AIComDestorySystem extends et.DestroySystem<AICom>{
    destroy(self: AICom): void {
        self.CancelToken?.Cancel();
        self.CancelToken = null;
        self.Current = "";
    }
}
class AIComHelper {
    public static Check(self: AICom) {
        for (const aiid of self.AIIds) {
            let aaihandler = AIDispatcherCom.Instance.AIHandlerMap.get(aiid);
            if (aaihandler == null) {
                console.error(`not found aihandler: ${aiid}`);
                continue;
            }
            let ret = aaihandler.Check(self);
            if (!ret) {
                continue;
            }

            if (self.Current == aiid) {
                break;
            }

            this.Cancel(self);
            let cancelToken = new et.CancelToken();
            self.CancelToken = cancelToken;
            self.Current = aiid;

            aaihandler.Execute(self, cancelToken);
            return;
        }
    }
    public static Cancel(self: AICom) {
        self.CancelToken?.Cancel();
        self.Current = "";
        self.CancelToken = null;
    }
}
